package validator

import (
	"regexp"
	"ucenter/app/model"
)

type LoginType string

const PHONE LoginType = "phone"
const FACEBOOK LoginType = "facebook"
const GOOGLE LoginType = "google"
const TOURIST LoginType = "tourist"

type LoginValidator struct {
	From       model.RegisterFrom `form:"from"`
	Phone      string             `form:"phone"`
	Avatar     string             `form:"avatar"`
	DeviceId   string             `form:"device_id"`
	NickName   string             `form:"nick_name"`
	GoogleId   string             `form:"google_id"`
	FacebookId string             `form:"facebook_id"`
	SmsCode    string             `form:"sms_code"`
	LoginType  LoginType          `form:"login_type"`
	IdToken    string             `form:"id_token"`
	Referrer   string             `from:"referrer"`
}

func (rs *LoginValidator) Valid() (res bool, msg string) {
	if rs.DeviceId == "" {
		return false, "device_id is empty"
	}
	if rs.LoginType != PHONE && rs.LoginType != TOURIST && rs.IdToken == "" {
		return false, "id_token is empty"
	}
	if rs.LoginType == "" {
		return false, "login_type is empty"
	}
	if (rs.LoginType == PHONE || rs.Phone != "") && rs.SmsCode == "" {
		return false, "sms_code is empty"
	}
	if rs.LoginType == PHONE && rs.Phone == "" {
		return false, "phone number can not be empty!"
	} else if rs.LoginType == FACEBOOK && rs.FacebookId == "" {
		return false, "facebook id can not be empty!"
	} else if rs.LoginType == GOOGLE && rs.GoogleId == "" {
		return false, "google id can not be empty!"
	}
	if rs.LoginType == "" {
		return false, "login_type is empty"
	}
	if rs.Referrer != "" && len(rs.Referrer) > 6 {
		return false, "invite code error"
	}
	return true, ""
}

type BindValidator struct {
	Type       LoginType `form:"type"`
	Phone      string    `form:"phone"`
	GoogleId   string    `form:"google_id"`
	FacebookId string    `form:"facebook_id"`
	SmsCode    string    `form:"sms_code"`
	Token      string    `form:"token"`
	IdToken    string    `form:"id_token"`
}

func (rs *BindValidator) Valid() (res bool, msg string) {
	if rs.Token == "" {
		return false, "phone can not be empty!"
	}
	if rs.Type == PHONE && rs.Phone == "" {
		return false, "phone can not be empty!"
	}
	if rs.Type != PHONE && rs.IdToken == "" {
		return false, "id_token is empty"
	}
	if rs.Type == FACEBOOK && rs.FacebookId == "" {
		return false, "facebook id can not be empty!"
	}
	if rs.Type == GOOGLE && rs.GoogleId == "" {
		return false, "google id can not be empty!"
	}
	return true, ""
}

type UpdateValidator struct {
	Email   string `form:"email"`
	Name    string `form:"name"`
	BankNo  string `form:"bank_no"`
	PayTm   string `form:"paytm"`
	Address string `form:"address"`
}

func (uv *UpdateValidator) Valid() (ok bool, msg string) {
	reg := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
	numReg := regexp.MustCompile(("^[0-9]{0,20}$"))
	if uv.Email != "" && !reg.MatchString(uv.Email) {
		return false, "email format error"
	}
	if uv.BankNo != "" && !numReg.MatchString(uv.BankNo) {
		return false, "bank card number format error"
	}
	if uv.PayTm != "" && !numReg.MatchString(uv.PayTm) {
		return false, "paytm number format error"
	}
	if uv.Name != "" && len(uv.Name) > 20 {
		return false, "name too long"
	}
	if uv.Address != "" && len(uv.Address) > 50 {
		return false, "address too long"
	}
	return true, ""
}

type OrderValidator struct {
	Channel string `form:"channel"`
	Amount  int    `form:"amount"`
	AppId   string `form:"app"`
}

func (ov *OrderValidator) Valid() (ok bool, msg string) {
	if ov.Amount == 0 {
		return false, "amount must bigger than 0"
	}
	if ov.Channel != "cashfree" && ov.Channel != "paytm" {
		return false, "pay channel error"
	}
	if ov.AppId == "" {
		ov.AppId = "rummy"
	}
	return true, ""
}
